<?php
class Project{
	
	public function init(){
		if(!is_dir(APPPATH.'data')){
			if($this->dir_writeable(APPPATH)==false){
				echo APPPATH." 文件夹不可写 请更改权限";
				exit;
			}
			$this->create_project();
		}
	}
	private function dir_writeable($dir) {
		if(!is_dir($dir)) {
			@mkdir($dir, 0777);
		}
		if(is_dir($dir)) {
			if($fp = @fopen("$dir/test.txt", 'w')) {
				@fclose($fp);
				@unlink("$dir/test.txt");
				$writeable = true;
			} else {
				$writeable = false;
			}
		}
		return $writeable;
	}
	private function mk_dir($dir, $mode = 0777) {
		if (is_dir($dir) || @mkdir($dir, $mode))
			return true;
		if (!$this->mk_dir(dirname($dir), $mode))
			return false;
		@mkdir($dir, $mode);
	}
	private function create_project(){
		$this->copy_file("RewriteEngine On\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^(.*)$ index.php/$1 [L]",APPPATH.'.htaccess',true);
		$this->create_data();
		$this->create_lib();
		$this->create_tpl();
		$this->create_application();
		$this->create_skin();
		if(ISADMIN==true){
			$this->create_control();
			$this->create_tool();
		}else{
			$this->create_index();
		}
		header('location:'.APPURL);
		exit;
	}
	
	private function create_data(){
		$this->mk_dir(APPDATA.'cnf/');
		$this->mk_dir(APPDATA.'cache/tpl/');
		if(ISADMIN==true){
			$this->mk_dir(APPDATA.'control/');
			$this->mk_dir(APPDATA.'control/list/');
			$this->mk_dir(APPDATA.'control/search/');
			$this->mk_dir(APPDATA.'control/edit/');
			$this->mk_dir(APPDATA.'cache/control/');
		}
		$this->create_htaccess(APPDATA);
		$this->create_data_file();
	}
	private function create_data_file(){	
		$data = array (
		  'debug' => true,
		  'tpl_path'=>'tpl',
		  'tpl_ext'=>'html',
		);
		if(ISADMIN==true){
			$data['list_tpl'] = 'list.html';
			$data['edit_tpl'] = 'edit.html';
			$data['control_path'] = 'control';
		}
		$this->create_cnf(APPDATA.'cnf/system.php',$data);
		
		$data = array (
			'default'=>array(
				  'host' => 'localhost',
				  'name' => 'gfphp',
				  'user' => 'root',
				  'password' => '123456',
			)
		);
		$this->create_cnf(APPDATA.'cnf/database.php',$data);
		
		$data = array ('gsql');
		$this->create_cnf(APPDATA.'cnf/tpl_tag.php',$data);
		
		$data = array ();
		$this->create_cnf(APPDATA.'cnf/autoload.php',$data);
		
		$data = array('app/([0-9]+)' => 'app/news/id/$1');
		$this->create_cnf(APPDATA.'cnf/routes.php',$data);
	}
	
	private function create_lib(){
		$this->mk_dir(APPPATH.'lib/');
		$this->create_htaccess(APPPATH.'lib/');
		$this->create_lib_file();
	}
	
	private function create_lib_file(){
		//file_put_contents(APPPATH.'lib/common.php',$this->get_examples('common'));
		//file_put_contents(APPPATH.'lib/tpl_tag.php',$this->get_examples('tpl_tag'));
		$this->copy_file($this->get_examples('common'),APPPATH.'lib/common.php',true);
		$this->copy_file($this->get_examples('tpl_tag'),APPPATH.'lib/tpl_tag.php',true);
	}
	
	private function create_tpl(){
		$this->mk_dir(APPPATH.'tpl/');
		$this->create_htaccess(APPPATH.'tpl/');
	}
	
	private function create_application(){
		$this->mk_dir(APPPATH.'application/');
		$this->create_htaccess(APPPATH.'application/');
		$this->copy_file($this->get_examples('app'),APPPATH.'application/app.php',true);
	}
	
	private function create_skin(){
		$this->mk_dir(APPPATH.'skin/');
		$this->mk_dir(APPPATH.'skin/css');
		$this->mk_dir(APPPATH.'skin/js');
		$this->mk_dir(APPPATH.'skin/image');
	}
	
	private function create_control(){
		$this->mk_dir(APPPATH.'control/');
		$this->create_htaccess(APPPATH.'control/');
		$this->mk_dir(APPPATH.'control/list/');
		$this->mk_dir(APPPATH.'control/search/');
		$this->mk_dir(APPPATH.'control/edit/');
	}
	
	private function create_tool(){
		$act = $this->get_examples('action');
		$admin = str_replace(array("main_act","runapp();"),array("Admin",""),$act);
		$admin = preg_replace("/public function index.*\}/sU","",$admin);
		//file_put_contents(APPPATH.'admin.php',$admin);
		$this->copy_file($admin,APPPATH.'admin.php',true);
		$index = str_replace(array("run.php","Action"),array("admin.php","Admin"),$act);
		$index = preg_replace("/public function init.*\}/sU","",$index);
		//file_put_contents(APPPATH.'index.php',$index);
		$this->copy_file($index,APPPATH.'index.php',true);
		$this->copy_dir(SYSPATH.'tool/',APPPATH.'tool/');
		$this->mk_dir(APPPATH.'tpl/tool/');
		//copy(APPPATH.'tool/index.html',APPPATH.'tpl/tool/index.html');
		//copy(APPPATH.'tool/list.html',APPPATH.'tpl/list.html');
		//copy(APPPATH.'tool/edit.html',APPPATH.'tpl/edit.html');
		$this->copy_file(APPPATH.'tool/index.html',APPPATH.'tpl/tool/index.html');
		$this->copy_file(APPPATH.'tool/list.html',APPPATH.'tpl/list.html');
		$this->copy_file(APPPATH.'tool/edit.html',APPPATH.'tpl/edit.html');
		unlink(APPPATH.'tool/index.html');
		unlink(APPPATH.'tool/list.html');
		unlink(APPPATH.'tool/edit.html');
	}
	
	private function create_index(){
		//file_put_contents(APPPATH.'index.php',$this->get_examples('action'));
		$this->copy_file($this->get_examples('action'),APPPATH.'index.php',true);
	}
	private function get_examples($name){
		return file_get_contents(SYSPATH.'data/examples/'.$name.'.php');
	}
	private function create_cnf($path,$data){
		$str = "<?php\nreturn ".var_export($data,true)."\n?>";
		//file_put_contents($path,$str);
		$this->copy_file($str,$path,true);
	}
	private function create_htaccess($path){
		//file_put_contents($path.'.htaccess',"order allow,deny\ndeny from all");
		$this->copy_file("order allow,deny\ndeny from all",$path.'.htaccess',true);
	}
	private function copy_dir($src,$dst) {  
	  
		$dir = opendir($src);
		@mkdir($dst);
		while(false !== ( $file = readdir($dir)) ) {
			if (( $file != '.' ) && ( $file != '..' )) {
				if ( is_dir($src . '/' . $file) ) {
					$this->copy_dir($src . '/' . $file,$dst . '/' . $file);
					continue;
				}
				else {
					//if(!is_file($dst . '/' . $file)) copy($src . '/' . $file,$dst . '/' . $file);
					$this->copy_file($src . '/' . $file,$dst . '/' . $file);
				}
			}
		}
		closedir($dir);
		
	}
	private function copy_file($src,$dst,$txt=false) {
		if(!is_file($dst)){
			if($txt==false){
				copy($src,$dst);
			}else{
				file_put_contents($dst,$src);
			}
		}
	}
}
?>